﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ZombieGame
{
    public class Drop : Entity
    {
        public int DropType;
        public override void Update(GameTime time)
        {
            Rotation += MathHelper.ToRadians(45f) * (float)time.ElapsedGameTime.TotalSeconds;
            for (int i = 0; i < ZombieGame.Current.World.Players.Count; i++)
            {
                Player p = ZombieGame.Current.World.Players[i];
                if (Drawable.CheckCollisionB(this, p))
                {
                    PickUp(p);
                    Kill();
                }
            }
            base.Update(time);
        }

        private void PickUp(Player p)
        {
            switch (DropType)
            {
                case 0:
                    p.Weapon.Ammo = p.Weapon.MaxAmmo;
                    break;
                case 1:
                    for (int i = 0; i < ZombieGame.Current.World.Zombies.Count; i++)
                        ZombieGame.Current.World.Zombies[i].Kill();
                    break;
                case 2:
                    p.Score += 150;
                    break;
                case 3:
                    int num = 0;
                    int ran = ZombieGame.Random.Next() % 20;
                    if (ZombieGame.Playtime.Elapsed.Minutes < 1)
                    {
                        if (ran > 4)
                            num = 1;
                        if (ran > 8)
                            num = 2;
                        if (ran > 11)
                            num = 3;
                        if (ran > 14)
                            num = 4;
                        if (ran > 16)
                            num = 5;
                        if (ran > 18)
                            num = 6;
                    }
                    else if (ZombieGame.Playtime.Elapsed.Minutes < 2)
                    {
                        if (ran > 3)
                            num = 1;
                        if (ran > 8)
                            num = 2;
                        if (ran > 11)
                            num = 3;
                        if (ran > 14)
                            num = 4;
                        if (ran > 16)
                            num = 5;
                        if (ran > 18)
                            num = 6;
                    }
                    else if (ZombieGame.Playtime.Elapsed.Minutes < 3)
                    {
                        if (ran > 2)
                            num = 1;
                        if (ran > 7)
                            num = 2;
                        if (ran > 11)
                            num = 3;
                        if (ran > 14)
                            num = 4;
                        if (ran > 16)
                            num = 5;
                        if (ran > 18)
                            num = 6;
                    }
                    else if (ZombieGame.Playtime.Elapsed.Minutes < 4)
                    {
                        if (ran > 1)
                            num = 1;
                        if (ran > 5)
                            num = 2;
                        if (ran > 10)
                            num = 3;
                        if (ran > 14)
                            num = 4;
                        if (ran > 16)
                            num = 5;
                        if (ran > 18)
                            num = 6;
                    }
                    else if (ZombieGame.Playtime.Elapsed.Minutes < 5)
                    {
                        if (ran > 0)
                            num = 1;
                        if (ran > 3)
                            num = 2;
                        if (ran > 7)
                            num = 3;
                        if (ran > 12)
                            num = 4;
                        if (ran > 16)
                            num = 5;
                        if (ran > 18)
                            num = 6;
                    }
                    else
                    {
                        if (ran > 0)
                            num = 1;
                        if (ran > 1)
                            num = 2;
                        if (ran > 4)
                            num = 3;
                        if (ran > 8)
                            num = 4;
                        if (ran > 13)
                            num = 5;
                        if (ran > 17)
                            num = 6;
                    }
                    p.Weapon = (Weapon)num;
                    break;
            }
        }

        public override float Layer
        {
            get { return 0.5f; }
        }

        public override object Clone()
        {
            Drop clone = new Drop(DropType);
            clone.Animation = this.Animation;
            clone.Health = this.Health;
            clone.Alive = this.Alive;
            clone.Color = this.Color;
            clone.ImageData = this.ImageData;
            clone.Velocity = this.Velocity;
            clone.Visible = this.Visible;
            clone.State = this.State;
            clone.Frame = this.Frame;
            clone.Position = this.Position;
            clone.Size = this.Size;
            clone.Scale = this.Scale;
            clone.Rotation = this.Rotation;
            clone.Origin = this.Origin;
            clone.Matrix = this.Matrix;
            return clone;
        }

        public Drop(int type)
            : base()
        {
            DropType = type;
            Animation.Add(ZombieGame.Current.Content.Load<Texture2D>("Zombie"));
            ImageData.Add(TextureToColor(Animation[0]));
            Origin = new Vector2(8f, 8f);
            Size = new Vector2(16f, 16f);
            Color = Color.Aqua;
        }
    }
}
